<?php
namespace app\common\validate\app;
use app\common\model\app\MerchantBlacklistModel;
use think\Validate;
/**
 * 买家黑名单验证器
 */
class MerchantBlacklistValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'store_platform_id' => 'require',
        'member_id' => 'require',
        'member_account_id' => ['require', 'checkAccountExisted'],
        'is_disable' => ['require', 'in' => '0,1'],
    ];
    // 错误信息
    protected $message = [
        'store_platform_id.require' => '店铺平台不能为空',
        'member_id.require' => '买家不能为空',
        'member_account_id.require' => '接单账号不能为空',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['store_platform_id','member_account_id','member_id'],
        'edit'    =>  ['id', ],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
    ];
    // 自定义验证规则：接单账号是否已存在
    protected function checkAccountExisted($value, $rule, $data = [])
    {
        $model = new MerchantBlacklistModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['member_account_id', '=', $data['member_account_id']];
        $where[] = ['store_platform_id', '=', $data['store_platform_id']];
        $where[] = ['member_id', '=', $data['member_id']];
        $where[] = ['mer_id', '=', mer_id()];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '该买家已拉黑';
        }
        return true;
    }
}
